﻿using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using SmartCity.Shared;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace CommonManagement
{
    /// <summary>
    /// 省份应用服务类
    /// </summary>
    public class ProvinceAppService : ApplicationService, IProvinceAppService
    {
        private readonly IRepository<Province, int> _provinceRepository;

        public ProvinceAppService(
            IRepository<Province, int> provinceRepository)
        {
            _provinceRepository = provinceRepository;
        }

        // 列表
        public async Task<ResultResponse<IList<ProvinceDto>>> ListAsync(string name = null)
        {
            // 查询
            var query = _provinceRepository
                .WhereIf(!string.IsNullOrWhiteSpace(name), m => m.Name.Contains(name));
            var list = await query.ToListAsync();
            // 返回结果
            var dtos = ObjectMapper.Map<List<Province>, List<ProvinceDto>>(list);
            return ResultResponse<IList<ProvinceDto>>.Successful(dtos);
        }
    }
}